package com.power.sifa.correction.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.plf.common.domain.PageDataResponse;
import com.plf.common.domain.ResponseDTO;
import com.plf.common.utils.ExcelUtil;
import com.power.sifa.correction.model.enumeration.St_work;
import com.power.sifa.correction.model.pageInfo.Cre_workPageInfo;
import com.power.sifa.correction.service.Cre_workService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.domain.Page;
import org.springframework.util.Assert;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import com.power.sifa.correction.model.Cre_work_record;
import com.power.sifa.correction.model.pageInfo.Cre_work_recordPageInfo;
import com.power.sifa.correction.api.ICre_work_recordFeign;
import com.power.sifa.correction.service.Cre_work_recordService;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;



/**
 *   处置记录控制器
 */

@Slf4j
//@PowerLog
@RestController
@Api(value = "Cre_work_recordController",tags={"处置记录"})
public class Cre_work_recordController implements ICre_work_recordFeign{

    @Resource
    private Cre_work_recordService cre_work_recordService;
    @Resource
    private Cre_workService cre_workService;


    @Override
    @ApiOperation("查询处置记录分页列表")
    @SaCheckPermission("cre_work_record_list")
    public ResponseDTO list(Cre_work_recordPageInfo pageInfo) {
        Assert.notNull(pageInfo, "pageInfo参数不能为空");
        Page<Cre_work_record> page = this.cre_work_recordService.findAll(pageInfo);
        List list = new ArrayList();
        if (!page.isEmpty()) {
            for (Cre_work_record cre_work_record : page) {
                cre_work_record.geneText();
                list.add(cre_work_record);
            }
        }
        PageDataResponse p = new PageDataResponse(list, page.getTotalElements(), page.getSize(), pageInfo.getPageNumber()<=1 ? 1 : pageInfo.getPageNumber());
        return ResponseDTO.succData(p);
    }

    @Override
    @ApiOperation("查看处置记录")
    @SaCheckPermission("cre_work_record_view")
    public ResponseDTO view(Integer id) {
        Assert.notNull(id, "id不能为空");
        Cre_work_record cre_work_record  = cre_work_recordService.getById(id);
        if (cre_work_record != null) {
            cre_work_record.geneText();
        }
        return ResponseDTO.succData(cre_work_record);
    }

    @Override
    @ApiOperation("删除处置记录")
    @SaCheckPermission("cre_work_record_del")
    public ResponseDTO delete(Integer id) {
        Assert.notNull(id, "id不能为空");
        cre_work_recordService.removeById(id);
        return ResponseDTO.succ();
    }

    @Override
    @ApiOperation("保存或修改处置记录")
    @SaCheckPermission("cre_work_record_save")
    public ResponseDTO save(Cre_work_record cre_work_record) {
        Assert.notNull(cre_work_record, "cre_work_record不能为空");
        // 默认给记录时间
        if(null == cre_work_record.getExectime()) {
            cre_work_record.setExectime(new Date());
        }
        cre_work_recordService.saveOrUpdate(cre_work_record);
        // 改成记录为处置中
        this.cre_workService.updateStatus(cre_work_record.getWorkid(), St_work.DOING);
        return ResponseDTO.succ();
    }

    @Override
    @ApiOperation(value="批量删除处置记录")
    @SaCheckPermission("cre_work_record_del")
    public ResponseDTO batchDel(String ids) {
        Assert.isTrue(StringUtils.hasText(ids), "ids不能为空");
        List<Integer> list = Arrays.asList(ids.split(",")).stream().map(s -> Integer.parseInt(s.trim())).collect(Collectors.toList());
        cre_work_recordService.removeByIds(list);
        return ResponseDTO.succ();
    }

    @Override
    @ApiOperation(value="导出excel")
    @SaCheckPermission("cre_work_record_list")
    public void output(HttpServletResponse response, Cre_work_recordPageInfo pageInfo) {
        Assert.notNull(pageInfo, "pageInfo不能为空");
        String title = "处置记录";
        String[] heads = {"workid_tm","记录时间","处置记录","记录人id"};
        String[] fields = {"id","exectime","record","userid"};
        Page<Cre_work_record> page = this.cre_work_recordService.findAll(pageInfo);
        ExcelUtil.exportXls(title, heads, fields, page.getContent(), response);
    }

    @Override
    @ApiOperation(value="导出excel模板")
    public void exportExcelTemplate(HttpServletResponse response) {
        String title = "处置记录";
        //id自增或list从表可自行删除，需与导入项一致
        String[] heads = {"workid_tm","记录时间","处置记录","记录人id"};
        ExcelUtil.exportXls(title, heads, null, null, response);
    }

	@Override
	@ApiOperation(value="导入excel")
	public ResponseDTO importExcel(MultipartFile file) throws IOException {
        Assert.notNull(file, "file不能为空");
        //id自增或list从表可自行删除，需与下载的模板中导入项一致
        String[] fields = {"id","exectime","record","userid"};
        List<Cre_work_record> list = (List<Cre_work_record>)ExcelUtil.Import(fields, file, Cre_work_record.class);
        if(!CollectionUtils.isEmpty(list)) {
            this.cre_work_recordService.saveAll(list);
        }
        return ResponseDTO.succ();
	}

    @Override
    public ResponseDTO queryList(Cre_work_recordPageInfo pageInfo) {
        try {
            return ResponseDTO.succData(this.cre_work_recordService.queryList(pageInfo));
        } catch (Exception e) {
            e.printStackTrace();
            log.error(" ->  msg[{}]", e.getMessage());
            return ResponseDTO.wrap(500, "");
        }
    }

    @Override
    public ResponseDTO findListByWarnId(String warnid) {
        try {
            return ResponseDTO.succData(this.cre_work_recordService.findListByWarnId(warnid));
        } catch (Exception e) {
            e.printStackTrace();
            log.error("/cre_work_record/findListByWarnId 根据预警id查询处置记录失败 ->  msg[{}]", e.getMessage());
            return ResponseDTO.wrap(500, "根据预警id查询处置记录失败");
        }

    }

}
